<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Ads_m extends FOX_Model
{

	function __construct ()
	{
		parent::__construct();
        $this->config->load('adsset');
        $this->config->load('categoryset');
	}

	public function get_all_ads(){
       return $this->db->select('ads.*,areas.area_name')->from('ads')->join('areas','areas.area_id=ads.ad_city')->get()->result_array();
        // $this->db->select('ads.*,areas.area_name');
        // $query = $this->db->where('area_id',$ad_city)->order('ad_id desc')->join('areas ON areas.area_id = ads.ad_city')->get();
        // $this->db->select('*');
        // $this->db->from('ads');
        // $query = $this->db->get();
        
    }

     public function sava_ads($id,$data){

        $this->db->where('ad_id',$id);
        $this->db->update('ads', $data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

    public function add_ads($data){

        $this->db->insert('ads',$data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }

    public function get_ads_by_ads_id($ad_id){
        $this->db->select('*');
        $query = $this->db->where('ad_id',$ad_id)->get('ads');
        return $query->row_array();
    }
    public function del_ads($id){
        // $this->db->where($where)->delete('citys');
        // return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
        $this->db->where('ad_id',$id)->delete('ads');
    }
    public function get_web_category(){
        $web_category=$this->web_category=explode('|', $this->config->item('web_category'));
        // var_dump($this->web_category);exit;
        $web_category_txt=$this->web_category_txt=explode('|', $this->config->item('web_category_txt'));
       $data['web_category']=array_combine($web_category,$web_category_txt);
       return  $data;
    }
    public function get_ad_type(){
        $ad_type=$this->ad_type=explode('|', $this->config->item('ad_type'));
        // var_dump($this->web_category);exit;
        $ad_type_text=$this->ad_type_text=explode('|', $this->config->item('ad_type_text'));
       $data['ad_type']=array_combine($ad_type,$ad_type_text);
       return  $data;
    }
    public function up_mycolumnist_pic()
    {
        $ad_id = $this->input->post('ad_id',TRUE);
        // $columnist_info = $this->columnist_m->get_columnist_by_id($columnist_id);
        if($_FILES){
            if($_FILES['ad_pic']['name']){
                $config['upload_path'] = FCPATH .'uploads/ads';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['encrypt_name'] = TRUE;
                $config['max_size'] = 3000;
                $pic_path = site_url('uploads/ads/');
                if(!file_exists($config['upload_path'])){
                    mkdir($config['upload_path'],0777,true);
                }
                $uptype=array('jpg','jpeg','png','gif');
                $torrent = explode(".", $_FILES['ad_pic']['name']);
                $fileend = strtolower(end($torrent));
                if(!in_array($fileend, $uptype))
                {
                    show_message('您的图片类型不正确','');
                };
                $this->load->library('upload', $config);
                if ($this->upload->do_upload('ad_pic'))
                {
                   $upload_data = $this->upload->data();
                    pp($upload_data);exit;
                    // echo $upload_data['upload_path'];
                    $this->columnist_m->update_columnist($columnist_id,array('columnist_icon'=>$columnist_id.'.jpg'));
                    // $arr = array ('status'=>1,'msg'=>'图片上传成功','icon'=>6,'code'=>$this->security->get_csrf_hash());
                    // echo json_encode($arr);
                    exit;
                }
            }else{
                $arr = array ('status'=>0,'msg'=>strip_tags($this->upload->display_errors()),'icon'=>5,'code'=>$this->security->get_csrf_hash());
                echo json_encode($arr);
                exit;
            }
        }else{
            $arr = array ('status'=>0,'msg'=>'图片上传失败','icon'=>5,'code'=>$this->security->get_csrf_hash());
            echo json_encode($arr);
            exit;
        }
    }
    public function resizeimg($source,$width,$height,$size) {
        $this->load->library('image_lib');
        $config['image_library'] = 'GD2';
        $config['source_image'] = $source;
        $config['new_image'] = $this->upload_path.'/'.$this->ad_id.'.jpg';
        $config['create_thumb'] = false;
        $config['maintain_ratio'] = true;
        $config['width'] = $width;
        $config['height'] = $height;
        $this->image_lib->initialize($config);
        $this->image_lib->resize();
        $this->image_lib->clear();
    }
    public function update_ad_pic($new_id,$data){
        $this->db->where('ad_id',$new_id);
        $this->db->update('ads', $data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }
    public function get_city_provence($area_type){
        $this->db->select('*');
        $query = $this->db->where('area_type',$area_type)->get("areas");
        return $query->result_array();
    }
    
}
